package com.ziytek.web.citizen.caffeine;

import com.github.benmanes.caffeine.cache.Cache;
import com.ziytek.web.citizen.config.caffeine.CaffeineUtil;
import io.netty.channel.ChannelHandlerContext;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author fengh
 */
@Slf4j
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class CaffeinePut {

    private final Cache<String, ChannelHandlerContext> caffeineCacheForChannel;

    public void putChannelContext(String key, ChannelHandlerContext value) {
        caffeineCacheForChannel.put(CaffeineUtil.IP_CACHE_PREFIX + key, value);
    }

    public ChannelHandlerContext getChannelContext(String key) {
        return caffeineCacheForChannel.getIfPresent(CaffeineUtil.IP_CACHE_PREFIX + key);
    }

    public List<String> channelContextKeys() {
        return caffeineCacheForChannel.asMap().keySet().stream().toList();
    }
}
